﻿using Base.FileRemoteModel.UpFile.Model;
using Basic.WxApiRemoteModel.WxMini.Model;
using Basic.WxApiService.Interface;
using Basic.WxExtendService.Interface;
using Basic.WxService.Interface;
using WeDonekRpc.Client.Attr;

namespace Basic.WxService.lmpl
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class MiniQrService : IMiniQrService
    {
        private readonly IWxMiniQrService _MiniQr;
        private readonly IFileApiService _File;
        private readonly IWxMerCacheService _WxMer;

        public MiniQrService (IWxMiniQrService miniQr,
            IFileApiService file,
            IWxMerCacheService wxMer)
        {
            this._MiniQr = miniQr;
            this._File = file;
            this._WxMer = wxMer;
        }

        public MiniQrResult CreateQr (string wxAppId, QrCreateParam param)
        {
            long wxMerId = this._WxMer.GetWxMerId(wxAppId);
            byte[] stream = this._MiniQr.CreateQr(wxAppId, param);
            FileSaveResult file = this._File.SaveFileStream(new FileSaveDatum
            {
                FileName = string.Concat("MiniQr_", wxAppId, ".png"),
                DirKey = "WxMiniQr",
                LinkBizPk = wxMerId
            }, stream);
            return new MiniQrResult
            {
                FileId = file.FileId,
                QrUri = file.FileUri
            };
        }
    }
}
